#include "Mesh/Vertex.h"


/************************************************
/*				Public
/************************************************/

    /************************************************
    /*				Contructors
    /************************************************/

        Vertex::Vertex(){

        }

        Vertex::Vertex(VertexTemplate & vt){
            m_VertexTemplate    =   &vt;
            m_Size              =   vt.size();
            m_Offset            =   vt.offset()+vt.sizeVertices();
            AllocateBuffer();
        }

    /************************************************
    /*				Methods
    /************************************************/

        int     Vertex::offset(){
            return m_Offset;
        }

        int     Vertex::size(){
            return m_Size;
        }

        void Vertex::SetTemplate(VertexTemplate * vt){

        }

        bool Vertex::SetAttribute(string name, float x){
            return SetAttribute(name,x,0,0,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y){
            return SetAttribute(name,x,y,0,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y, float z){
            return SetAttribute(name,x,y,z,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y, float z, float w){
            bool found=false;
            int i=0;
            float args[4]= {x,y,z,w};

            while(!found && i < m_VertexTemplate->attributeCount()){
                if(m_VertexTemplate->attribute(i).name()==name){

                    for(int j=0; j< 4; j++){
                        if(m_VertexTemplate->attribute(i).size()>j){
                            BufferVertices.at(offset()+
                                              m_VertexTemplate->attribute(i).offset()+j)=args[j];
                        }
                    }
                    found=true;
                }
                i++;
            }
            return found;
        }

        bool Vertex::SetAttribute(int pos, float x){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y, float z){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y, float z, float w){
            float args[4]= {x,y,z,w};

            if(pos< m_VertexTemplate->attributeCount()){
                for(int j=0; j< 4; j++){
                    if(m_VertexTemplate->attribute(pos).size()>j){
                        BufferVertices.at(offset()+
                                          m_VertexTemplate->attribute(pos).offset()+j)=args[j];
                    }
                }
            }
            return false;

        }

/************************************************
/*				Statics
/************************************************/

    /************************************************
    /*				Properties
    /************************************************/

             vector<GLfloat>      Vertex::BufferVertices;
             vector<Vertex *>     Vertex::Vertices;

    /************************************************
    /*				Methods
    /************************************************/

             void Vertex::Debug(){

                 qDebug()<< " size : "<<BufferVertices.size();
                 for(int i=0; i< BufferVertices.size();i+=4){

                     qDebug()<<BufferVertices.at(i) <<" "<<BufferVertices.at(i+1) <<" "<<BufferVertices.at(i+2)<< " "<<BufferVertices.at(i+3);



                 }
             }

             float * Vertex::Array(){
                 return &BufferVertices[0];
             }

/************************************************
/*				Members
/************************************************/

    void Vertex::AllocateBuffer(){

        if(BufferVertices.size()==offset()){
            for(int i=0; i< size(); i++){
                BufferVertices.push_back(0.0f);
            }
        }else{
            for(int i=0; i< size(); i++){
                BufferVertices.insert(BufferVertices.begin()+offset()+i,  0.0f);
            }
        }
    }

    void Vertex::ComputeNormal()
    {
        vec4 sum;

        /*for(int i=0;i<m_Faces.size();i++)
        {
            sum+=m_Faces.at(i)->m_Normal;
        }

        sum= sum/m_Faces.size();
 //       m_Normal = (sum).Normalize();*/
    }




